<!-- cart component -->
<template>
  <div class="gz-cart">
    <!-- 商品展示 -->
    <div class="mui-card" v-for="(item, i) in goodsList" :key="item.id">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <div class="gz-ct-body">
            <mt-switch v-model="$store.getters.getSelectedById[item.id]" 
              @change="selectedChanged(item.id)"></mt-switch>
            <img :src="item.thumb_path" />
            <div class="gz-ct-content">
              <h1 class="gz-ct-title" v-text="item.title"></h1>
              <div class="gz-ct-content-dd">
                <span class="gz-ct-price">￥{{ item.sell_price }}</span>
                <numbox :initval="$store.getters.getCountById[item.id]" :gid="item.id"></numbox>
                <a href="#" @click="deleteGoods(item.id, i)">删除</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 结算 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <div class="pay">
            <div class="left">
              <p>总计(不含运费):</p>
              <p>已勾选商品 <span class="red"> {{ $store.getters.getTotalCountAndPrice.totalCount }}</span> 件，总价 <span class="red">￥{{ $store.getters.getTotalCountAndPrice.totalPrice }}</span></p>
            </div>  
            <mt-button type="danger">去结算</mt-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import numbox from "../cart/cart-numbox.vue";
export default {
  data() {
    return {
      goodsList: []
    };
  },

  created() {
    this.getGoodsList();
  },

  methods: {
    // 获取商品详情列表
    getGoodsList() {
      // 遍历store 获取商品id
      let ids = this.$store.getters.getIds;
      if (ids.length === 0) return;
      this.axios.get("api/goods/getshopcarlist/" + ids.join(",")).then(resp => {
        if (resp.data.status === 0) {
          // 获取数据成功
          this.goodsList = resp.data.message;
        } else {
          // 获取失败
          Toast("获取数据失败");
        }
      });
    },
    // 删除购物车中商品，同步store
    deleteGoods(gid, i) {
      this.goodsList.splice(i, 1);
      this.$store.commit("deleteCart", gid);
    },
    // 开关
    selectedChanged(gid) {
      this.$store.commit('updateSelected', gid);
    }
  },

  components: {
    numbox
  }
};
</script>

<style lang='scss' scoped>
.gz-cart {
  background-color: #eee;
  overflow: hidden;
  .gz-ct-body {
    display: flex;
    flex-direction: row;
    align-items: center;
    img {
      width: 60px;
      height: 60px;
    }
    .gz-ct-content {
      .gz-ct-title {
        font-size: 13px;
      }
      .gz-ct-content-dd {
        .gz-ct-price {
          color: red;
        }
      }
    }
  }
  .pay {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;

    .red {
      color: red;
    }
  }
}
</style>